<?php

require_once __DIR__ . '/../vendor/autoload.php';

require_once __DIR__ . '/Mailer.php';
require_once __DIR__ . '/UserManager.php';
require_once __DIR__ . '/UserCon.php';
require_once __DIR__ . '/UserRepository.php';

// use DI\Container;
use DI\Bridge\Slim\Bridge;
// use Psr\Http\Message\ServerRequestInterface as Request;
// use Psr\Http\Message\ResponseInterface as Response;
// use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
// use Slim\Routing\RouteCollectorProxy;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use Slim\Factory\ServerRequestCreatorFactory;

// use Slim\Factory\AppFactory;



// $app = Bridge::create();
$containerBuilder = new ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->useAnnotations(true);

try {
    /** @var ContainerInterface $container */
    $container = $containerBuilder->build();
} catch (\Exception $e) {
    die("容器构建失败: " . $e->getMessage());
}
// AppFactory::setContainer($container);
// $app = AppFactory::create();
$app = Bridge::create($container);


// 6. 路由配置
// $app->get('/users', [UserController::class, 'index']);


// require_once __DIR__ . '/Middleware1.php';
// require_once __DIR__ . '/Middleware2.php';
// require_once __DIR__ . '/RouteMiddleV1.php';
// require_once __DIR__ . '/RouteRegex.php';
require_once __DIR__ . '/RouteGroup.php';





// global error handler
require_once __DIR__ . '/HttpErrorHandler.php';
require_once __DIR__ . '/ShutdownHandler.php';
$displayErrorDetails = true;
$callableResolver = $app->getCallableResolver();
$responseFactory = $app->getResponseFactory();
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();
$errorHandler = new HttpErrorHandler(callableResolver: $callableResolver, responseFactory: $responseFactory);
$shutdownHandler = new ShutdownHandler(request: $request, errorHandler: $errorHandler, displayErrorDetails: $displayErrorDetails);
register_shutdown_function(callback: $shutdownHandler);


$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, false, false);
$errorMiddleware->setDefaultErrorHandler($errorHandler);

$app->run();

?>